<?php

namespace App\Http\BizServices;

use App\Http\Request\AdConfigListRequest;
use Illuminate\Support\Facades\DB;

class AdConfigBizService extends BaseService
{
    public function adConfigList(AdConfigListRequest $request)
    {
        $collection = $this->all($request->locationList);
        $adList = [];
        foreach ($collection as $item) {
            $adList[] = [
                'id' => $item->id,
                'adv_id' => $item->adv_id,
                'title' => $item->title,
                'position' => $item->position,
                'cover_image' => $item->cover_image,
                'click_url' => $item->click_url,
                'click_type' => $item->click_type,
                'resource_url' => $item->resource_url,
                'resource_type' => $item->resource_type,
                'play_duration' => $item->play_duration,
                'display_duration' => $item->display_duration,
                'is_skip' => $item->is_skip,
                'skip_url' => $item->skip_url,
                'skip_resource_url' => $item->skip_url,
            ];
        }
        return $adList;
    }

    private function all($locationList = [])
    {
        $collection = DB::table('advertisements as a')
            ->when($locationList, function ($query) use ($locationList) {
                $query->whereIn('a.placement', $locationList);
            })
            ->where('a.status', 'active')
            ->get();
        return $collection;
    }
}
